package com.example.moshudriver.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;

import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.fragment.A1_HomeFragment;
import com.example.moshudriver.fragment.A2_MenuFragment;
import com.example.moshudriver.fragment.RightFragment;
import com.example.moshudriver.fragment.SlidingMenu;
import com.example.moshudriver.tools.LocationManager;
import com.example.moshudriver.tools.MessageConstant;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.external.eventbus.EventBus;

public class SlidingActivity extends FragmentActivity {

	public static final String RESPONSE_METHOD = "method";
	public static final String RESPONSE_CONTENT = "content";
	public static final String RESPONSE_ERRCODE = "errcode";
	protected static final String ACTION_LOGIN = "com.baidu.pushdemo.action.LOGIN";
	public static final String ACTION_MESSAGE = "com.baiud.pushdemo.action.MESSAGE";
	public static final String ACTION_RESPONSE = "bccsclient.action.RESPONSE";
	public static final String ACTION_PUSHCLICK = "bccsclient.action.PUSHCLICK";
	public static final String ACTION_SHOW_MESSAGE = "bccsclient.action.SHOW_MESSAGE";
	protected static final String EXTRA_ACCESS_TOKEN = "access_token";
	public static final String EXTRA_MESSAGE = "message";
	public static final String CUSTOM_CONTENT = "CustomContent";

	public static final String ACTION_SHOW_MESSAGE_LIST = "ACTION_SHOW_MESSAGE_LIST";

	private SlidingMenu mSlidingMenu;
	private A2_MenuFragment mA2MenuFragment;
	private RightFragment mRightFragment;
	private Fragment mA1HomeFragment;

	private Fragment mFragment;

	private SharedPreferences mShared;
	private SharedPreferences.Editor mEditor;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.main);
		handleIntent(getIntent());

		init();

		mShared = this.getSharedPreferences(O2OMobileAppConst.USERINFO, 0);
		mEditor = mShared.edit();
		EventBus.getDefault().register(this);
	}

	private void init() {
		mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu);

		mSlidingMenu.setLeftView(getLayoutInflater().inflate(
				R.layout.left_frame, null));
		mSlidingMenu.setRightView(getLayoutInflater().inflate(
				R.layout.right_frame, null));
		mSlidingMenu.setCenterView(getLayoutInflater().inflate(
				R.layout.center_frame, null));

		FragmentTransaction t = this.getSupportFragmentManager()
				.beginTransaction();
		mA2MenuFragment = new A2_MenuFragment();
		t.replace(R.id.left_frame, mA2MenuFragment);

		mRightFragment = new RightFragment();
		t.replace(R.id.right_frame, mRightFragment);

		mA1HomeFragment = new A1_HomeFragment();
		t.replace(R.id.center_frame, mA1HomeFragment);
		t.commitAllowingStateLoss();

	}

	public void showLeft() {
		mSlidingMenu.showLeftView();
	}

	public void showRight() {
		mSlidingMenu.showRightView();
	}

	public void switchContent(Fragment fragment) {

		this.mFragment = fragment;

		FragmentTransaction t = this.getSupportFragmentManager()
				.beginTransaction();

		t.replace(R.id.center_frame, this.mFragment);
		t.commitAllowingStateLoss();

		showLeft();

	}

	public void isShowLeft(boolean Left) {
		mSlidingMenu.setCanSliding(Left, false);
	}

	// �˳�����
	private boolean isExit = false;

	@SuppressLint("NewApi")
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mSlidingMenu.mScroller.getCurrX() != 0) {
				showLeft();
				return true;
			} else {
				if (isExit == false) {
					isExit = true;
					ToastView toast = new ToastView(getApplicationContext(),
							getString(R.string.exit_again));
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					handler.sendEmptyMessageDelayed(0, 3000);
					return true;
				} else {
					LocationManager.getInstance().stopLocation();//停止更新地址
					finish();
					
					return false;
				}
			}
		}
		return true;
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onNewIntent(Intent intent) {
	}

	private void handleIntent(Intent intent) {
		// Todo
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		// getSupportFragmentManager().beginTransaction().remove(mFragment);
		super.onDestroy();
	}

	public void onEvent(Object event) {
		Message message = (Message) event;
		if (message.what == MessageConstant.LOGINOUT) {
			finish();
		}
	}
}
